package com.sx.gongzhong.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sx.gongzhong.BaseActivity;
import com.sx.gongzhong.R;
import com.sx.gongzhong.adapter.MessageNotifyAdapter;
import com.sx.gongzhong.model.MessageBean;
import com.sx.gongzhong.model.MsgEvent;
import com.sx.gongzhong.net.MyRxBus;
import com.sx.gongzhong.pest.MessageNotifyPest;
import com.sx.gongzhong.utils.AppConstants;
import com.sx.gongzhong.utils.ToastUtils;
import com.sx.gongzhong.view.MessageNotifyView;
import com.sx.gongzhong.widget.LoadDialog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 通知
 * Created by admin on 2018/1/3.
 */

public class MessageNotifyActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener, MessageNotifyView {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;
    @BindView(R.id.ll_buttom)
    LinearLayout llButtom;

    private int page = 1, pagesize = 10,readPosition = -1;
    private MessageNotifyAdapter adapter;
    private List<MessageBean> mlist;

    @Override
    public void setUI(Bundle savedInstanceState) {
        setContentView(R.layout.activity_message);
        ButterKnife.bind(this);
        setTitleName("通知");
        initView();
    }

    private void initView() {
        mlist = new ArrayList<>();
        if (swipeRefreshLayout != null) {
            swipeRefreshLayout.setColorSchemeResources(AppConstants.colors);
            swipeRefreshLayout.setOnRefreshListener(this);
        }
        recyclerView.setLayoutManager(new LinearLayoutManager(MessageNotifyActivity.this, LinearLayoutManager.VERTICAL, false));
        adapter = new MessageNotifyAdapter(this, R.layout.item_message_notify, mlist);
        adapter.setEmptyView(View.inflate(this, R.layout.load_error_view, null));
        recyclerView.setAdapter(adapter);
        adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                page++;
                getData();
            }
        }, recyclerView);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (!mlist.get(position).readState){
                    readPosition = position;
                    new MessageNotifyPest(MessageNotifyActivity.this).readMessageNotify(mlist.get(position).id);
                }
                switch (mlist.get(position).columnType){
                    case "convenient_appeal": //诉求类型
                        startActivity(new Intent(MessageNotifyActivity.this,ConvenniceAppealDetailActivity.class)
                        .putExtra("appealId",mlist.get(position).columnDataId));
                        break;
                    case "broke_news"://爆料类型
                        startActivity(new Intent(MessageNotifyActivity.this,ExposureDetailActivity.class)
                                .putExtra("ExposureID",mlist.get(position).columnDataId));
                        break;
                    case "help_each_other"://互帮互助
                        startActivity(new Intent(MessageNotifyActivity.this,HelpDetailActivity.class)
                                .putExtra("helpId",mlist.get(position).columnDataId));
                        break;
                    case "national_road_protection"://全民护路
                        startActivity(new Intent(MessageNotifyActivity.this,DefendRoadDetailActivity.class)
                                .putExtra("protectionId",mlist.get(position).columnDataId));
                        break;
                    case "legal_advice"://法律咨询
                        startActivity(new Intent(MessageNotifyActivity.this,LegalAdviceDetailActivity.class)
                                .putExtra("adviceId",mlist.get(position).columnDataId));
                        break;
                }
            }
        });
        onRefresh();
    }

    @Override
    public void onRefresh() {
        swipeRefreshLayout.setRefreshing(false);
        page = 1;
        getData();
    }

    private void getData() {
        new MessageNotifyPest(this).getData(page,pagesize);
    }

    @OnClick({R.id.tv_all_read, R.id.tv_all_clear})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_all_read:
                StringBuilder sbs = new StringBuilder();
                for (MessageBean bean : mlist){
                    sbs.append(bean.id+",");
                }
                new MessageNotifyPest(this).readMessageNotify(sbs.toString());
                break;
            case R.id.tv_all_clear:
                LoadDialog.show(MessageNotifyActivity.this,"删除中...");
                StringBuilder sb = new StringBuilder();
                for (MessageBean bean : mlist){
                    sb.append(bean.id+",");
                }
                new MessageNotifyPest(this).deleteMessageNotify(sb.toString());
                break;
        }
    }

    @Override
    public void getSuccess(List<MessageBean> list,int total) {
        if (page == 1) {
            mlist.clear();
            adapter.setNewData(mlist);
        }
        if (list != null && list.size() > 0) {
            mlist.addAll(list);
        }
        if (page == total){
            adapter.loadMoreEnd();
        }else {
            adapter.loadMoreComplete();
        }
        adapter.notifyDataSetChanged();
        if (mlist.size() == 0){
            llButtom.setVisibility(View.GONE);
        }else {
            llButtom.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void deleteMessageNotifySuccess(String result) {
        MyRxBus.get().post(new MsgEvent<>("MessageFragment",1,null));
        if (result.equals("删除成功")){
            LoadDialog.dismiss(MessageNotifyActivity.this);
            mlist.clear();
            adapter.notifyDataSetChanged();
            page = 1;
            getData();
        }else {
            if (readPosition >= 0){//本地更改一个已读
                mlist.get(readPosition).readState = true;
                readPosition = -1;
            }else {
                for (int i = 0; i < mlist.size(); i++) {
                    mlist.get(i).readState = true;
                }
            }
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void showError(String message) {
        LoadDialog.dismiss(MessageNotifyActivity.this);
        ToastUtils.showToast(MessageNotifyActivity.this,message);
    }
}
